package com.lebang.service;

import com.lebang.domain.AndroidVersionSdkError;
import com.lebang.example.AndroidVersionSdkErrorExample;
import com.lebang.mapper.AndroidVersionSdkErrorMapper;
import com.lebang.mapper.SdkErrorLogMapper;
import com.lebang.model.MemberSdkErrorModel;
import com.lebang.model.cences.CencesMsg;
import com.lebang.service.mongodb.CencesMsgMongodb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class AndroidSdkErrorService {

    @Autowired
    private AndroidVersionSdkErrorMapper androidVersionSdkErrorMapper;
    @Autowired
    private SdkErrorLogMapper sdkErrorLogMapper;
    @Autowired
    private CencesMsgMongodb cencesMsgMongodb;

    public long countByExample(AndroidVersionSdkErrorExample example) {
        return androidVersionSdkErrorMapper.countByExample(example);
    }


    public List<MemberSdkErrorModel> selectByExample(AndroidVersionSdkErrorExample example) {

        List<MemberSdkErrorModel> models = androidVersionSdkErrorMapper.selectByExa(example);
        example.setLimit(null);
        example.setOffset(null);
        example.setOrderByClause(null);
        List<MemberSdkErrorModel> all = androidVersionSdkErrorMapper.selectByExa(example);
        AtomicInteger num = new AtomicInteger(0);
        all.forEach(s -> {
            num.addAndGet(s.getNum());
        });
        models.forEach(s -> {
            if (num.get() == 0) {
                s.setRate(BigDecimal.ZERO);
            } else {
                s.setRate(BigDecimal.valueOf(s.getNum()).divide(BigDecimal.valueOf(num.get()), 2, RoundingMode.HALF_DOWN));
            }
        });
        return models;
    }

    /**
     * 手机型号数据源错误码统计
     *
     * @param end
     */
    public void starDataInit(LocalDateTime end) {
        LocalDate now = end.toLocalDate().minusDays(1);
        long starTime = System.currentTimeMillis();
        //star 开始日期  end 结束日期
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");
        int time = Integer.parseInt(end.toLocalDate().minusDays(1).format(df));

        //结束日期
        LocalDateTime endDateTime = LocalDateTime.of(end.toLocalDate(), LocalTime.MIN);
        //开始日期
        LocalDateTime starDateTime = endDateTime.minusDays(1);
        //统计用户数据源错误码
        List<MemberSdkErrorModel> memberSdkErrorModels = sdkErrorLogMapper.cencusVersionSdkErrorByDateTime(starDateTime, endDateTime);
        for (MemberSdkErrorModel memberSdkErrorModel : memberSdkErrorModels) {
            AndroidVersionSdkError androidVersionSdkError = new AndroidVersionSdkError();
            androidVersionSdkError.setCreateDate(now);
            androidVersionSdkError.setAndroidVersion(memberSdkErrorModel.getAndroidVersion());
            androidVersionSdkError.setErrorCode(memberSdkErrorModel.getErrorCode());
            androidVersionSdkError.setSdkKey(memberSdkErrorModel.getSdkKey());
            androidVersionSdkError.setNum(memberSdkErrorModel.getNum());
            androidVersionSdkErrorMapper.insertSelective(androidVersionSdkError);
        }
        cencesMsgMongodb.save(new CencesMsg("手机型号数据源错误码统计", (System.currentTimeMillis() - starTime)));

    }
}
